package org.gxuwz.core.dao.impl;

import java.util.List;

import org.gxuwz.core.dao.ICommissureDao;
import org.gxuwz.core.entity.TCommissure;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("CommissureDao")
public class CommissureDao extends BaseDao implements ICommissureDao{
	
	@Autowired
	private SessionFactory sessionFactory;
	public Session getSession() {
		Session session = sessionFactory.openSession();
		return session;
	}

	@Override
	public void addCommissureDao(TCommissure comm) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(comm);
		tx.commit();
		session.close();
	}

	//@Override
	@SuppressWarnings("unchecked")
	public List<TCommissure> getAll(String comcode, String perscode) {
		List<TCommissure> list = null;
		Session session = getSession();
		String hql = "select * from t_commissure where 1=1";
		//String hql = "from TFamily  where 1=1";
		if(perscode!=null&&comcode!=""){
			hql += " and perscode like '%"+perscode+"%'";
		}
		if(comcode!=null&&comcode!=""){
			hql += " and comcode like '%"+comcode+"%'";
		}
		Query query = session.createSQLQuery(hql).addEntity(TCommissure.class);
		//Query query = session.createQuery(hql);
		list = query.list();
		session.close();
		return list;
	}

	@Override
	public TCommissure getByPersCode(String perscode) {
		//System.out.println(perscode+"yyyyyyyyyyyyyyyyyyyy");
		TCommissure files = null;
		List<TCommissure> list = null;
		Session session = getSession();
		String hql = "select * from t_commissure where 1=1";
		//String hql = "from TFamily  where 1=1";
		if(perscode!=null&&perscode!=""){
			hql += " and perscode like '%"+perscode+"%'";
		}
		Query query = session.createSQLQuery(hql).addEntity(TCommissure.class);
		list = query.list();
		session.close();
		if(list.size()>=0){
			files =list.get(0);
		}
	    return files;
	}

	@Override
	public void update(TCommissure comm) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.update(comm);
		tx.commit();
		session.close();
	}

	@Override
	public void delete(TCommissure comm) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.delete(comm);
		tx.commit();
		session.close();
	}

}
